package com.leyou.item.servcie.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.servcie.SpecParamService;
import com.leyou.item.servcie.SpuDetailService;
import com.leyou.item.servcie.SpuService;
import com.leyou.pojo.dto.SpecParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpuService spuService;


    /**
     * 查询规格参数
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDto> querySpecParam(Long categoryId, Long groupId, Boolean searching) {

        List<SpecParam> specParamList = query().eq(categoryId!=null, "category_id", categoryId)
                .eq(groupId!=null, "group_id", groupId)
                .eq(searching!=null, "searching", searching)
                .list();
        List<SpecParamDto> paramDtoList = specParamList.stream().map(SpecParamDto::new).collect(Collectors.toList());
        return paramDtoList;
    }

    /**
     * 修改规格参数
     * @param specParamDto
     */
    @Override
    @Transactional
    public void updateParam(SpecParamDto specParamDto) {
        //修改规格参数只涉及到一张表，因此只需要先转换在修改即可
        //装换Dto
        SpecParam specParam = specParamDto.toEntity(SpecParam.class);
        //修改规格参数
        boolean b = updateById(specParam);
        if(b==false){
            throw  new LyException("修改规格参数异常",500);
        }
    }

    /**
     * 新增规格参数
     * @param specParamDto
     */
    @Override
    @Transactional
    public void saveParam(SpecParamDto specParamDto) {
        //这个也是只需要涉及一张表即可
        //转换Dto
        SpecParam specParam = specParamDto.toEntity(SpecParam.class);
        //新增规格参数
        boolean b = save(specParam);
        if(b==false){
            throw new LyException("新增规格参数失败",500);
        }
    }

    /**
     * 根据spuId查询所有规格参数值
     * @param spuId
     * @return
     */
    @Override
    public List<SpecParamDto> queryParamValue(Long spuId,Boolean searching) {
        //此次查询设计两张表，spuDetail，specParam表
        //根据spuId查询出规格参数值
        SpuDetail spuDetail = spuDetailService.getById(spuId);
        String specification = spuDetail.getSpecification();
        //将string转换成map
//        Map<Long, Object> objectMap = JsonUtils.toMap(specification, Long.class, Object.class);
        Map<Long, Object> objectMap = JsonUtils.toMap(specification, Long.class, Object.class);
        //根据商品id查出对应规格参数的key
        //先查询出分类id
        Long cid3 = spuService.getById(spuId).getCid3();
        //查询规格参数key
        List<SpecParamDto> specParamDtoList = querySpecParam(cid3, null, searching);
        //遍历集合，将map结合的key与查询到的参数集合的id一一对应
        for (SpecParamDto specParamDto : specParamDtoList) {
            Long id = specParamDto.getId();
            Object value = objectMap.get(id);
            if(value!=null) {
                specParamDto.setValue(value);
            }
        }



        return specParamDtoList;
    }
}
